local message_packet = {}

local proto_manager = require "common.proto_manager"

function message_packet.pack(id,buf)

    local m = {
        id = id,
        data = buf or nil,
    }
    local data = proto_manager.encode("MessageHeader",m)

    return string.pack(">I2",string.len(data)) .. data

end

function message_packet.unpack(msg,sz)

    local header,err = proto_manager.decode("MessageHeader",msg,sz)

    return header,err

end

function message_packet.packSendBuffer(id,t,message)
    local _id = id
    if type(id) == "string" then
        _id = proto_manager.message_id(id)
    end
    local pack
    if t then
        local buf = proto_manager.encode(t,message)
        pack = message_packet.pack(_id,buf)
    else
        pack = message_packet.pack(_id)
    end
    return pack
end

return message_packet